
#include "luas.h"

static lua_State *s_pLuaState;

int luas_init()
{
    s_pLuaState = lua_open();

	luaL_openlibs(s_pLuaState); /* opens the basic library */

    return 1;
}

int luas_close()
{
    lua_close(s_pLuaState);

    s_pLuaState = NULL;
}

int luas_reg_lib(const char *libname, luaL_Reg *reglist)
{
	luaL_register(s_pLuaState, libname, (luaL_Reg*)reglist);

	return 0;
}

int luas_dofile(const char *filepath)
{
    int rt = luaL_dofile(s_pLuaState, filepath);
    if(rt != 0)
    {
        js_sys("luac", "lua file:%s error:%s\n", filepath, lua_tostring(s_pLuaState, -1));
    }
    return rt;
}


int luas_classfunc(int self, const char *func, const char *format, ...){
    va_list   args;
    va_start(args, format);

    int it = luas_vcallfunc(self, func, format, args);
    va_end(args);
    return it;
}
int luas_callfunc(const char *func, const char *format, ...)
{
    va_list   args;
    va_start(args, format);

    int it = luas_vcallfunc(0, func, format, args);
    va_end(args);
    return it;
}
int luas_vcallfunc(int self, const char *func, const char *format, va_list args)
{
   // js_sys("luac", "luac_vcallfunc:%s\n", func);
    int ir;
    int i, arg_num = 0;


   // js_sys("luac", "func name:%s\n", func);
    int format_len = strlen(format);
    int func_len = strlen(func);

    int is_table_function = 0;
    for(i = 0; i < func_len; i++){
    	if(func[i] == '.')
    	{
    		is_table_function = i;
    		break;
    	}
    }
    if(is_table_function != 0){
    	char newfunc[64];
    	strcpy(newfunc, func);
    	newfunc[is_table_function] = '\0';
    	char *table_name = newfunc;
    	char *member_name = newfunc + is_table_function + 1;
    	lua_getglobal(s_pLuaState, table_name);//压入table
    	lua_pushstring(s_pLuaState, member_name);//压入key
    	lua_gettable(s_pLuaState, -2);//取value
    	lua_remove(s_pLuaState, -2);//弹出table
    }else{
    	lua_getglobal(s_pLuaState, func);
    }
     
    if(self != 0){
        lua_rawgeti(s_pLuaState, LUA_REGISTRYINDEX, self);
        arg_num++;
    }
    for(i = 0; i < format_len; i++)
    {
        char c = format[i];
        if(c != '%')continue;

        c = format[i+1];
        if(c == 'd')
        {
            int val = va_arg(args,int);
            lua_pushnumber(s_pLuaState, val);
          //  js_sys("luac", "lua_pushnumber %d\n", val);
            arg_num++;
        }else if(c == 's')
        {
            char *val = va_arg(args, char*);
            lua_pushstring(s_pLuaState, val);
          //  js_sys("luac", "lua_pushstring %s\n", val);
            arg_num++;
        }
    }
    if (lua_pcall(s_pLuaState, arg_num, 1, 0) != 0)
	{
		goto error;
	}

	ir = lua_gettop(s_pLuaState);

	if (ir > 0)
	{
		int tr = (int)lua_tonumber(s_pLuaState, -1);

		lua_pop(s_pLuaState, ir);

		return tr;
	}
error:
    js_sys("luac", "error running function %s: %s\n", func, lua_tostring(s_pLuaState, -1));
	return 0;
}
